package my.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Credits;
import my.bean.Customer;
import my.dao.ICreditsDao;

public class ICreditDaoImpl implements ICreditsDao {
	private Connection conn;

	public ICreditDaoImpl() {

	}

	public ICreditDaoImpl(Connection conn) {
		this.conn = conn;
	}

	public void setConnection(Connection conn) {
		this.conn = conn;
	}

	@Override
	public int add(Credits credit) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "insert into credits values(credits_id.nextval,?,?,?,?)";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, credit.getCid());
			prestmt.setInt(2, credit.getType());
			prestmt.setDouble(3, credit.getValue());
			prestmt.setTimestamp(4, new Timestamp(credit.getTime().getTime()));

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("增加积分记录成功!");
			} else {
				System.out.println("增加积分记录失败,请重新操作!");
			}

			return record;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int modidy(Credits credit) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "update credits set customer_id=?,credits_type=?,credits_value=?,credits_time=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, credit.getCid());
			prestmt.setInt(2, credit.getType());
			prestmt.setDouble(3, credit.getValue());
			prestmt.setTimestamp(4, new Timestamp(credit.getTime().getTime()));

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("信息更新成功!");
			} else {
				System.out.println("信息更新失败,请重新操作");
			}

			return record;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int deleteById(int cid) throws SQLException {
		PreparedStatement prestmt = null;

		try {
			String sql = "";
			prestmt = conn.prepareStatement(sql);

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
		return 0;
	}

	@Override
	public List<Credits> findAll() {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		List<Credits> list = new ArrayList<>();
		try {
			String sql = "select * from credits join customer on customer_id=user_id";
			prestmt = conn.prepareStatement(sql);
			rs = prestmt.executeQuery();

			while (rs.next()) {
				int id = rs.getInt("CID");
				int cId = rs.getInt("USER_ID");
				int type = rs.getInt("CREDITS_TYPE");
				double value = rs.getDouble("CREDITS_VALUE");
				Timestamp time1 = rs.getTimestamp("CREDITDS_TIME");
				String account = rs.getString("user_account");
				String email = rs.getString("user_mail");
				String phone = rs.getString("user_phone");
				Timestamp time2 = rs.getTimestamp("register_date");
				Customer customer = new Customer(cId, account, null, email, phone, time2);

				Credits credit = new Credits(id, customer, type, value, time1);
				list.add(credit);
			}
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return list;
	}

	@Override
	public List<Credits> findByUserID(int userid) {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		List<Credits> list = new ArrayList<>();
		try {
			String sql = "select * from credits join customer on customer_id=user_id where customer_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, userid);
			rs = prestmt.executeQuery();

			while (rs.next()) {
				int id = rs.getInt("CID");
				int cId = rs.getInt("USER_ID");
				int type = rs.getInt("CREDITS_TYPE");
				double value = rs.getDouble("CREDITS_VALUE");
				Timestamp time1 = rs.getTimestamp("CREDITS_TIME");
				String account = rs.getString("user_account");
				String email = rs.getString("user_email");
				String phone = rs.getString("user_phone");
				Timestamp time2 = rs.getTimestamp("register_date");
				Customer customer = new Customer(cId, account, null, email, phone, time2);

				Credits credit = new Credits(id, customer, type, value, time1);
				list.add(credit);
			}
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return list;
	}

	@Override
	public Credits findByCId(int cid) {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		Credits credit = null;
		try {
			String sql = "select * from credits where cid=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, cid);
			rs = prestmt.executeQuery();

			if (rs.next()) {
				int id = rs.getInt("CID");
				int cId = rs.getInt("USER_ID");
				int type = rs.getInt("CREDITS_TYPE");
				double value = rs.getDouble("CREDITS_VALUE");
				Timestamp time1 = rs.getTimestamp("CREDITDS_TIME");
				String account = rs.getString("user_account");
				String email = rs.getString("user_mail");
				String phone = rs.getString("user_phone");
				Timestamp time2 = rs.getTimestamp("register_date");
				Customer customer = new Customer(cId, account, null, email, phone, time2);

				credit = new Credits(id, customer, type, value, time1);
			}
			return credit;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return credit;

	}
}
